    <?php
	set_time_limit(0); 
	include ("laytin.php");
	header('Content-Type: text/html;charset=utf-8');
    //check the url is ok 
	 function urlOk($url) {
		$headers = @get_headers($url);
		if($headers[0] == 'HTTP/1.1 200 OK') return true;
		else return false;
	}
	//post new entry to my blogspot
     function createPublishedPost($title, $content){
      global $gdClient,$blogID;
      $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default';
      $entry = $gdClient->newEntry();
      $entry->title = $gdClient->newTitle($title);
      $entry->content = $gdClient->newContent($content);
      $entry->content->setType('text');
      $createdPost = $gdClient->insertEntry($entry, $uri);
      $idText = split('-', $createdPost->id->text);
      $newPostID = $idText[2];
      return $newPostID;
    }
    //get account of blogspot    
    $xml = simplexml_load_file("config.xml");
	$user = $xml->user;
	$pass = $xml->pass;
	$blogID = $xml->blogid;
	//apply for big category
	$category = $xml->category;
	//apply for title
	$number = $xml->number;
	//echo $user."</br>".$pass."</br>".$blogID."</br>".$number;
	//start Zend library
    $service = 'blogger';     
    require_once'Zend/Loader.php';
    Zend_Loader::loadClass('Zend_Gdata');
    Zend_Loader::loadClass('Zend_Gdata_Query');
    Zend_Loader::loadClass('Zend_Gdata_ClientLogin');     
    $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null,
                            Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null,
                            Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE');							
    $gdClient = new Zend_Gdata($client);
    //get  category link from dantri.com.vn  category
	$urlwebsite = $category;
	$links=DanTri_TrangChu($urlwebsite);
	$content = "";
	//get sub link from dantri.com.vn 
	$link_array = array_values($links);
	//echo $link_array;
	$url = str_replace($category,"http://dantri.com.vn",$link_array[intval($number)]);
	
	if(urlOk($url))
		{
		$tin=DanTri_Lay1Tin($urlwebsite,$url);		
		$title = $tin['tieude'];
		$summary = '<p><strong>'.$tin['tomtat'].'</strong></p>';
		//remove the tags from dantri
		$description = preg_replace('/(<div class="news-tag-list".+?)+(<\/div>)/i', '', $tin['content']); 
		//add more time for number
		$xml->number = (intval($number)+1)%12;
		$xml->asXML("config.xml");
		//post into my blogspot
		createPublishedPost($title,$summary.$description);
		//for testing		
		//echo $title.$summary.$description;
		return true;
		}
    ?>